﻿#include <iostream>
#include <sstream>
#include <string>
#include <cstdlib>
#include <vector>
#include "XmlMap.h"
#include "tinyxml.h"
#include "tinystr.h"

using namespace std;

Map::Map(int h, int w)
{
	//string temp = "<Map><Height>";
	//stringstream ss;
	//ss << height;
	//temp += ss.str() + "</Height><Width>";
	//ss.clear();
	//ss << width;
	//temp += ss.str(); + "</Width></Map>";

	const char* mapXml = "<Map></Map>";

	mapDoc.Parse(mapXml);

	if(mapDoc.Error())
	{
		cout << "Harita olusturma hatasi!" << endl;
	}

	height = h;
	width = w;
}

Map::Map(const char* mapXml)
{
	mapDoc.Parse(mapXml);
	if(mapDoc.Error())
	{
		cout << "Harita olusturma hatasi!" << endl;
	}
}

void Map::SaveMap(const char* fileName)
{
	mapDoc.SaveFile(fileName);
}

void Map::RealizeMap()
{
	TiXmlNode* node = 0;
	TiXmlElement* mainElement = 0;
	TiXmlElement* itemElement = 0;
	TiXmlElement* cellElement = new TiXmlElement("Cells");
	TiXmlElement* objectElement = new TiXmlElement("Objects");
	TiXmlElement* questionElement = new TiXmlElement("Questions");


	float coord_x;
	float coord_y;
	float coord_z;
	float scale_x;
	float scale_y;
	float scale_z;
	float red;
	float green;
	float blue;
	float angle;

	node = mapDoc.FirstChild("Map");
	assert(node);
	mainElement = node -> ToElement();
	assert(mainElement);

	//Odanýn genel özellikleri alýnýyor
	node = mainElement->FirstChildElement("Attributes");
	itemElement = node->ToElement();
	assert(itemElement);
	this->width = atof(itemElement->Attribute("width"));
	this->height = atof(itemElement->Attribute("height"));
	this->name = itemElement->Attribute("name");
	//ilk oda yaratýlýyor

	vector<float> walls;
	vector<vector<float>> cellVector;
	node = mainElement->FirstChildElement("Cells");
	node = node->FirstChild();
	assert(node);
	cellElement = node->ToElement();
	assert(cellElement);
	int size;
	//Cell etiketleri içinde dolaşacak
	while(cellElement ->NextSiblingElement())
	{
		cellElement = node->ToElement();
		assert(cellElement);
		size = atof(cellElement->Attribute("size"));
		node = node->FirstChild()->FirstChild();
		assert(node);
		itemElement = node->ToElement();
		assert(itemElement);

		walls.clear();
		coord_x = atof(itemElement->Attribute("coord_x"));
		walls.push_back(coord_x);
		coord_y = atof(itemElement->Attribute("coord_y"));
		walls.push_back(coord_y);
		coord_z = atof(itemElement->Attribute("coord_z"));	
		walls.push_back(coord_z);
		scale_x = atof(itemElement->Attribute("scale_x"));
		walls.push_back(scale_x);
		scale_y = atof(itemElement->Attribute("scale_y"));
		walls.push_back(scale_y);
		scale_z = atof(itemElement->Attribute("scale_z"));
		walls.push_back(scale_z);
		red = atof(itemElement->Attribute("red"));
		walls.push_back(red);
		green = atof(itemElement->Attribute("green"));
		walls.push_back(green);
		blue = atof(itemElement->Attribute("blue"));
		walls.push_back(blue);
		angle = atof(itemElement->Attribute("angle"));
		walls.push_back(angle);

		cellVector.push_back(walls);

		
		//Wall etiketleri içinde dolaşacak
		while(itemElement->NextSiblingElement())
		{
			itemElement = itemElement->NextSiblingElement();
			assert(itemElement);

			walls.clear();
			coord_x = atof(itemElement->Attribute("coord_x"));
			walls.push_back(coord_x);
			coord_y = atof(itemElement->Attribute("coord_y"));
			walls.push_back(coord_y);
			coord_z = atof(itemElement->Attribute("coord_z"));	
			walls.push_back(coord_z);
			scale_x = atof(itemElement->Attribute("scale_x"));
			walls.push_back(scale_x);
			scale_y = atof(itemElement->Attribute("scale_y"));
			walls.push_back(scale_y);
			scale_z = atof(itemElement->Attribute("scale_z"));
			walls.push_back(scale_z);
			red = atof(itemElement->Attribute("red"));
			walls.push_back(red);
			green = atof(itemElement->Attribute("green"));
			walls.push_back(green);
			blue = atof(itemElement->Attribute("blue"));
			walls.push_back(blue);
			angle = atof(itemElement->Attribute("angle"));
			walls.push_back(angle);

			cellVector.push_back(walls);
		}	

		node = node->Parent()->Parent();
		assert(node);
		cellElement = node->ToElement();
		assert(cellElement);
		if(node->NextSibling())
		{
		node = node->NextSibling();
		assert(node);
		}
	}
	node = mainElement ->FirstChildElement("Objects");
	assert(node);
	objectElement = node->FirstChild()->ToElement();
	assert(objectElement);
	vector<std::string> objects;
	vector<vector<std::string>> objectVector;
	while(objectElement)
	{
		objects.clear();
		objects.push_back(objectElement->Attribute("path"));
		objects.push_back(objectElement->Attribute("texture"));
		objects.push_back(objectElement->Attribute("coord_x"));
		objects.push_back(objectElement->Attribute("coord_y"));
		objects.push_back(objectElement->Attribute("coord_z"));
		objects.push_back(objectElement->Attribute("size_x"));
		objects.push_back(objectElement->Attribute("size_y"));
		objects.push_back(objectElement->Attribute("size_z"));
		objectVector.push_back(objects);
		objectElement = objectElement ->NextSiblingElement();
	}

    node = mainElement ->FirstChildElement("Questions");
	assert(node);
	questionElement = node->FirstChild()->ToElement();
	assert(questionElement);
	vector<std::string> questions;
    vector<vector<std::string>> questionVector;
	while(questionElement)
	{
		questions.clear();
		questions.push_back(questionElement->Attribute("question"));
		questions.push_back(questionElement->Attribute("answer"));
		questionVector.push_back(questions);
		questionElement = questionElement ->NextSiblingElement();
	}

	this->questionVector = questionVector;
	this->cellVector = cellVector;
	this->objectVector = objectVector;
}

vector<vector<float>> Map::getCellVector()
{
	return this->cellVector;
}

vector<vector<std::string>> Map::getObjectVector()
{
	return this->objectVector;
}

vector<vector<std::string>> Map::getQuestionVector()
{
	return this->questionVector;
}

